package com.commpany.servie.basic.clickhistory.service.read;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.ArrayList;
import com.commpany.servie.basic.clickhistory.api.read.ClickHistoryReadService;
import com.commpany.jdq.core.common.PageResult;
import com.commpany.jdq.core.orm.Pagination;
import com.commpany.servie.basic.clickhistory.business.read.clickHistory.ClickHistoryReadManage;
import com.commpany.servie.basic.clickhistory.model.dto.clickhistory.ClickHistoryDTO;
import com.commpany.servie.basic.clickhistory.converter.ClickHistoryConverter;
import com.commpany.servie.basic.clickhistory.po.ClickHistoryPO;
import com.commpany.servie.basic.clickhistory.model.dto.PageResultDTO;
@Service("clickHistoryReadService")
public class ClickHistoryReadServiceImpl  implements ClickHistoryReadService {
	@Autowired
	private ClickHistoryReadManage clickHistoryReadManage;
	
	public ClickHistoryDTO getClickHistoryService(ClickHistoryDTO inputDTO){
	
	   return ClickHistoryConverter.toDTO(clickHistoryReadManage.getClickHistoryService(ClickHistoryConverter.toPO(inputDTO)));
	}
   @Override
	public PageResultDTO<ClickHistoryDTO> selectClickHistoryPageService(ClickHistoryDTO inputDTO){

		ClickHistoryPO po = ClickHistoryConverter.toPO(inputDTO);
		Pagination page = new Pagination();
	    page.setLimitStart(inputDTO.getLimitStart());
		page.setPageNo(inputDTO.getPageNo());
		page.setPageSize(inputDTO.getPageSize());
		Optional.ofNullable(inputDTO.getOrderBy()).ifPresent(page::setOrderBy);
		PageResult<ClickHistoryPO> poPage = clickHistoryReadManage.selectClickHistoryPageService( po,page);
		List<ClickHistoryPO> poList = poPage.getList();
		if(Objects.isNull(poList)) {
	    	 return PageResultDTO.<ClickHistoryDTO>builder().build();
	     }		
		 List<ClickHistoryDTO> dtoList = poList.stream().map(src-> ClickHistoryConverter.toDTO(src)).collect(Collectors.toList());
		return 	PageResultDTO.<ClickHistoryDTO>builder().pageNo(poPage.getPageNo()).pageSize(poPage.getPageSize()).list(dtoList).build();
	}
   @Override
    public int countClickHistoryService( ClickHistoryDTO inputDTO ){
         ClickHistoryPO po = ClickHistoryConverter.toPO(inputDTO);
         return clickHistoryReadManage.countClickHistoryService(po);
    }
     @Override
	public List<ClickHistoryDTO> queryClickHistoryService (ClickHistoryDTO inputDTO){
	    ClickHistoryPO po = ClickHistoryConverter.toPO(inputDTO);
	     List<ClickHistoryPO> list = clickHistoryReadManage.queryClickHistoryService(po);
		   if(Objects.isNull(list)) {
	    	 return new ArrayList<ClickHistoryDTO>();
	     }
		return list.stream().map(src-> ClickHistoryConverter.toDTO(src)).collect(Collectors.toList());
	}
}
	